/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CrearUsuarioInternalFrame.java
 *
 * Created on 7/10/2011, 08:22:57 AM
 */

package proyectosudp;


import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import proyectosudp.src.ClienteUDP;
import proyectosudp.src.Usuario;

/**
 *
 * @author ANALISTA
 */
public class CrearUsuarioInternalFrame extends javax.swing.JInternalFrame {
    ClienteUDP cliente = null;
    String[] roles = { "Gestor", "Profesor investigador", "Vicerrector"};
    private String[] unidades = null;
    private String cedula;


    /** Creates new form CrearUsuarioInternalFrame */
    public CrearUsuarioInternalFrame(ClienteUDP cliente, String[] unidades) {
        try {
            initComponents();
            this.cliente = cliente;
            this.unidades = unidades;
            this.cedula = cliente.getCedula();
            //unidades = cliente.consultarUnidades().split("#");
            //jComboBox2 = new JComboBox(unidades);
            jComboBox2.removeAllItems();
            for(int i =1; i<unidades.length;i++)
                jComboBox2.addItem(unidades[i]);
            //jComboBox2.setModel(jComboBox2.getModel());
        } catch (Exception ex) {
            Logger.getLogger(CrearUsuarioInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox(roles);
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        setTitle("Crear usuario");
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(null);

        jLabel1.setText("Cedula:");
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(30, 30, 100, 14);

        jLabel2.setText("Nombre:");
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(30, 60, 120, 14);

        jLabel3.setText("Rol:");
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel1.add(jLabel3);
        jLabel3.setBounds(30, 90, 100, 14);

        jLabel4.setText("Privilegios:");
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel1.add(jLabel4);
        jLabel4.setBounds(30, 120, 120, 14);

        jLabel5.setText("Unidad académica:");
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel1.add(jLabel5);
        jLabel5.setBounds(30, 150, 150, 14);

        jTextField2.setName("jTextField2"); // NOI18N
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        jPanel1.add(jTextField2);
        jTextField2.setBounds(190, 30, 240, 20);

        jTextField3.setName("jTextField3"); // NOI18N
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });
        jPanel1.add(jTextField3);
        jTextField3.setBounds(190, 60, 240, 20);

        jTextField5.setEditable(false);
        jTextField5.setName("jTextField5"); // NOI18N
        jTextField5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField5ActionPerformed(evt);
            }
        });
        jPanel1.add(jTextField5);
        jTextField5.setBounds(190, 120, 310, 20);

        jComboBox1.setSelectedIndex(1);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Gestor", "Profesor investigador", "Vicerrector" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel1.add(jComboBox1);
        jComboBox1.setBounds(190, 90, 240, 20);

        jComboBox2.setName("jComboBox2"); // NOI18N
        jPanel1.add(jComboBox2);
        jComboBox2.setBounds(190, 150, 240, 20);

        jButton1.setText("Guardar");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(150, 200, 100, 23);

        jButton3.setText("Nuevo");
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(270, 200, 100, 23);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 90, 540, 250);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(null);

        jButton2.setText("Consultar");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);
        jButton2.setBounds(200, 20, 100, 23);

        jLabel6.setText("Cedula");
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel2.add(jLabel6);
        jLabel6.setBounds(20, 20, 90, 14);

        jTextField1.setText("111");
        jTextField1.setName("txtCedulaConsultar"); // NOI18N
        jPanel2.add(jTextField1);
        jTextField1.setBounds(90, 20, 100, 20);

        jLabel7.setName("jLabel7"); // NOI18N
        jPanel2.add(jLabel7);
        jLabel7.setBounds(310, 10, 220, 40);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(10, 20, 540, 60);

        jLabel8.setName("jLabel8"); // NOI18N
        getContentPane().add(jLabel8);
        jLabel8.setBounds(10, 350, 330, 14);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField5ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jTextField5ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        if(jComboBox1.getSelectedItem().equals("Gestor")){
            jTextField5.setText("Consulta");
        }else{
            jTextField5.setText("Consulta/Creación/Actualización");
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        //ClienteUDP cliente = new ClienteUDP();
        Usuario usuario = new Usuario();
        if(jTextField2.getText()!=null && !jTextField2.getText().equals("")&&
                jTextField3.getText()!=null && !jTextField3.getText().equals("")&&
                jTextField5.getText()!=null && !jTextField5.getText().equals("")){
            usuario.setCedula(jTextField2.getText());
            usuario.setNombre(jTextField3.getText());
            usuario.setRol(jComboBox1.getSelectedItem().toString());
            usuario.setPrivilegio(jTextField5.getText());
            usuario.setUnidadAcademica(jComboBox2.getSelectedItem().toString().split("-")[0].trim());
//            usuario.setCedulaLog(cedula);
            try {
                if(jTextField2.isEditable()){
                    cliente.crearUsuario(usuario, "crear");
                    JOptionPane.showMessageDialog(this, "Usuario creado correctamente");
                }else{
                    cliente.crearUsuario(usuario, "editar");
                    JOptionPane.showMessageDialog(this, "Usuario modificado correctamente");
                }
                dispose();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "No se pudo realizar la operación");
                Logger.getLogger(CrearUsuarioInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            // TODO add your handling code here:
            String usuario = cliente.consultarUsuario(jTextField1.getText());
            if(usuario == null){
                JOptionPane.showMessageDialog(this, "Usuario no existente");
            }else{
//            System.out.println(usuario);
                String[] arr = usuario.split("#");
                if(!arr[1].equals("%")){
                    jTextField2.setText(arr[1]);
                    jTextField2.setEditable(false);
                    jTextField3.setText(arr[2]);
                    jComboBox1.setSelectedItem(arr[3]);
                    if(arr[4].equalsIgnoreCase("C")){
                        jTextField5.setText("Consulta");
                    }else{
                        jTextField5.setText("Consulta/Creación/Actualización");
                    }
                    for(int i =1; i<unidades.length;i++){
        //                System.out.println(unidades[i]);
        //                System.out.println(unidades[i].split("-")[0].trim());
                        if(arr[5].equalsIgnoreCase(unidades[i].split("-")[0].trim())){
                            jComboBox2.setSelectedIndex(i-1);
                        }
                    }
                }else
                    JOptionPane.showMessageDialog(this, "Usuario no existente");
            }
            
            //usuarioConsultado#111#Ferchis#Vicerrector#E#null#

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "No se pudo realizar la operación");
            Logger.getLogger(CrearUsuarioInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        jTextField2.setEditable(true);
    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the unidades
     */
    public String[] getUnidades() {
        return unidades;
    }


}
